package com.tszs.Controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.PersonalOrderBiz;
import com.tszs.entity.PersonalOrder;
import com.tszs.entity.PersonalOrderExample;
import com.tszs.util.CallBack;
import com.tszs.util.DateUtils;

import net.sf.json.JSONObject;

/**
 * @类名:MemberOrderController
 * @类描述：会员订单
 * @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class MemberOrderController {
	/**
	 * @AdministratorTableBiz
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private PersonalOrderBiz personalOrderBiz;

	/**
	 * @方法名:memberOrderselectPage
	 * @方法描述：订单查询 
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 * @throws Exception 
	 *
	 */
	@RequestMapping("pc/admin/memberOrderselectPage")
	public void memberOrderselectByExamplePage(HttpServletResponse response, HttpServletRequest request) throws Exception {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String cName = "";
		if(StringUtils.isNotEmpty(request.getParameter("cName"))) {
			cName = URLEncoder.encode(request.getParameter("cName"));
		}
		String begin=request.getParameter("type3");
		String end=request.getParameter("type4");
		PersonalOrder example = new PersonalOrder();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		example.setConsignee(cName);
		if(!(null==begin)&&!(null==end)) {
			if(!"".equals(begin)&&!"".equals(end)){
				begin=begin+" 00:00:01";
				end=end+" 23:59:59";
				example.setBegin(begin);
				example.setEnd(end);
			}
		}
			example.setpStatus("0");
		List<PersonalOrder> selectByExample = personalOrderBiz.selectByExamplePage(example);
		PersonalOrder example1 = new PersonalOrder();
		int page2=0;
		int limit2=0;
		example1.setPage((page2 - 1) * limit2);
		example1.setLimit(limit2);
		if(!(null==begin)&&!(null==end)) {
			if(!"".equals(begin)&&!"".equals(end)){
				begin=begin+" 00:00:01";
				end=end+" 23:59:59";
				example1.setBegin(begin);
				example1.setEnd(end);
			}
		}
			example1.setpStatus("0");
			List<PersonalOrder> selectByExample2 = personalOrderBiz.selectByExamplePage(example1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample2.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			PersonalOrder order = selectByExample.get(i);
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("cId", i + 1);
			mapData.put("uId", URLDecoder.decode(order.getuId(), "UTF-8"));
			mapData.put("pCreateTime", order.getpCreateTime());
			mapData.put("ProductId", order.getProductId());
			mapData.put("TotalCommodityPrice", order.getTotalCommodityPrice());
			if("1".equals(order.getPayStatic())) {
				mapData.put("PayStatic","已支付");
				if("0".equals(order.getPayMethod())){
					mapData.put("PayMethod","");
				}else if("1".equals(order.getPayMethod())){
					mapData.put("PayMethod","微信支付");
				}else {
					mapData.put("PayMethod","支付宝支付");
				}
			}else {
				mapData.put("PayStatic","未支付");
				mapData.put("PayMethod","未支付");
			}
			String button = "<a href=\"javascript:table('2','"
					+ order.getpId() + "')\" class=\"layui-btn layui-btn-xs\">删除</a><a href=\"javascript:table('0','"
					+ order.getpId() + "')\"  class=\"layui-btn layui-btn-danger layui-btn-xs\">查看详情</a>";
			if("1".equals(order.getRoutinStatus())) {
				button += "<a href=\"javascript: post('"+order.getpId()+"')\" class=\"layui-btn layui-btn-success layui-btn-xs\">确认发货</a>";
			}
			mapData.put("button", button);
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}
	/**
	 * @方法名:memberOrderDelete
	 * @方法描述：订单伪删除
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/memberOrderDelete")
	public void memberOrderDelete(HttpServletResponse response, HttpServletRequest request) {
		PersonalOrder record=new PersonalOrder();
		record.setpStatic("1");//1删除0正常
		PersonalOrderExample example=new PersonalOrderExample();
		com.tszs.entity.PersonalOrderExample.Criteria ca=example.createCriteria();
		ca.andPIdEqualTo(request.getParameter("id"));
		int updateByExampleSelective=personalOrderBiz.updateByExampleSelective(record, example);
		if(updateByExampleSelective>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	
	/**
	 * 发货
	 * @author Fujing
	 * @date 2019年5月31日 下午2:37:33
	 * @param response
	 * @param request void
	 */
	@RequestMapping("pc/admin/memberOrderPost")
	public void memberOrderPost(HttpServletResponse response, HttpServletRequest request) {
		PersonalOrder record=new PersonalOrder();
		record.setRoutinStatus("2");
		record.setExpressno(request.getParameter("expressno"));
		record.setUpdatetime(DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_FULL));
		PersonalOrderExample example=new PersonalOrderExample();
		com.tszs.entity.PersonalOrderExample.Criteria ca=example.createCriteria();
		ca.andPIdEqualTo(request.getParameter("id"));
		int updateByExampleSelective=personalOrderBiz.updateByExampleSelective(record, example);
		if(updateByExampleSelective>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	
	/**
	 * @方法名:memberOrderSelectAll
	 * @方法描述：订单详情
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/memberOrderSelectAll")
	public void memberOrderSelectAll(HttpServletResponse response, HttpServletRequest request) {
		PersonalOrder example=new PersonalOrder();
		example.setpId(request.getParameter("id"));
		PersonalOrder t = personalOrderBiz.orderForPage(example);
		t.setPayStatic("1".equals(t.getPayStatic())?"待支付":"已支付");
		
		if (t.getPayMethod() == null){
			t.setPayMethod("待支付");
		} else if("0".equals(t.getPayMethod())) {
			t.setPayMethod("微信");
		} else if("1".equals(t.getPayMethod())){
			t.setPayMethod("支付宝");
		} else {
			t.setPayMethod("银联");
		}
		t.setReceivingAddress(t.getReceivingAddress());
		if (t.getRoutinStatus() == null) {
			t.setRoutinStatus("");
		} else if("1".equals(t.getRoutinStatus())) {
			t.setRoutinStatus("发货中");
		}else if("2".equals(t.getRoutinStatus())) {
			t.setRoutinStatus("货物发出");
		}else if("3".equals(t.getRoutinStatus())) {
			t.setRoutinStatus("确认收货");
		}else if("4".equals(t.getRoutinStatus())) {
			t.setRoutinStatus("退货");
		}else {
			t.setRoutinStatus("退货完成");
		}
		new CallBack().yes(response, t, "");
	}
	
	
	/**
	 * @方法名:OrderselectPage
	 * @方法描述：订单查询 
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/OrderselectPage")
	public void OrderselectPage(HttpServletResponse response, HttpServletRequest request) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String begin=request.getParameter("type3");
		String end=request.getParameter("type4");
		String routinStatus=request.getParameter("type5");
		String pStatus=request.getParameter("type6");
		String payStatic=request.getParameter("type7");
		PersonalOrder example = new PersonalOrder();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		if(!(null==begin)&&!(null==end)) {
			if(!"".equals(begin)&&!"".equals(end)){
				begin=begin+" 00:00:01";
				end=end+" 23:59:59";
				example.setBegin(begin);
				example.setEnd(end);
			}
		}
		if(!"".equals(routinStatus)&&routinStatus!=null){
			example.setRoutinStatus(routinStatus);
		}
		if(!"".equals(pStatus)&&pStatus!=null){
			example.setpStatus(pStatus);
		}
		if(!"".equals(payStatic)&&payStatic!=null){
			example.setPayStatic(payStatic);
		}
		
		List<PersonalOrder> selectByExample = personalOrderBiz.OrderselectPage(example);
		PersonalOrder example1 = new PersonalOrder();
		int page2=0;
		int limit2=0;
		example1.setPage((page2 - 1) * limit2);
		example1.setLimit(limit2);
		if(!(null==begin)&&!(null==end)) {
			if(!"".equals(begin)&&!"".equals(end)){
				begin=begin+" 00:00:01";
				end=end+" 23:59:59";
				example1.setBegin(begin);
				example1.setEnd(end);
			}
		}
		if(!"".equals(routinStatus)&&routinStatus!=null){
			example1.setRoutinStatus(routinStatus);
		}
		if(!"".equals(pStatus)&&pStatus!=null){
			example1.setpStatus(pStatus);
		}
		if(!"".equals(payStatic)&&payStatic!=null){
			example1.setPayStatic(payStatic);
		}
		List<PersonalOrder> selectByExample1 = personalOrderBiz.OrderselectPage(example1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample1.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i= 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("cId", i + 1);
			mapData.put("pId", selectByExample.get(i).getpId());
			mapData.put("PayStatic", selectByExample.get(i).getPayStatic().equals("0")?"待支付":"已支付");
			mapData.put("TotalCommodityPrice", selectByExample.get(i).getTotalCommodityPrice());
			mapData.put("pCreateTime", selectByExample.get(i).getpCreateTime());
			mapData.put("ReceivingAddress", selectByExample.get(i).getReceivingAddress());
			mapData.put("Expressno", selectByExample.get(i).getExpressno());
			mapData.put("Updatetime", selectByExample.get(i).getUpdatetime());
			/*mapData.put("pStatus", selectByExample.get(i).getpStatus().equals("0")?"线下支付":"在线支付");*/
			if(selectByExample.get(i).getPayMethod().equals("0")) {
				mapData.put("PayMethod","微信");
			}else if(selectByExample.get(i).getPayMethod().equals("1")){
				mapData.put("PayMethod","支付宝");
			}else {
				mapData.put("PayMethod","银联");
			}
			if(selectByExample.get(i).getRoutinStatus().equals("1")) {
				mapData.put("RoutinStatus","发货中");
			}else if(selectByExample.get(i).getRoutinStatus().equals("2")) {
				mapData.put("RoutinStatus","货物发出");
			}else if(selectByExample.get(i).getRoutinStatus().equals("3")) {
				mapData.put("RoutinStatus","确认收货");
			}else if(selectByExample.get(i).getRoutinStatus().equals("4")) {
				mapData.put("RoutinStatus","退货");
			}else {
				mapData.put("RoutinStatus","退货完成");
			}
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}
}
